package com.tledu.studentmanager.controller;

import com.tledu.studentmanager.domain.Student;
import com.tledu.studentmanager.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author cyrus
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @RequestMapping("/list")
    public String studentList(String name, String school, Model model) {
        List<Student> list = studentService.selectList(name, school);
        model.addAttribute("list", list);
        return "student/list";
    }

    @RequestMapping("/addPage")
    public String addPage() {
        return "student/add";
    }

    @RequestMapping("/add")
    public String addStudent(Student student) {
        studentService.insert(student);
        return "redirect:/student/list";
    }

    @RequestMapping("/updatePage")
    public String updatePage(Integer id,Model model) {
        Student student = studentService.selectOne(id);
        model.addAttribute("student", student);
        return "student/update";
    }

    @RequestMapping("update")
    public String updateStudent(Student student) {
        studentService.update(student);
        return "redirect:/student/list";
    }


    @RequestMapping("/delete")
    public String deleteStudent(int id) {
        studentService.delete(id);
        return "redirect:/student/list";
    }
}

